<?php
/**
 * 抽奖日志
 */
namespace app\common\model;
use app\admin\model\MemberLevel as MemberLevelModel;
use app\admin\model\Member as MemberModel;
use think\Model;

class DrawLog extends Model
{
    protected $name = 'draw_log';

    public static function getList($orderby,$limit,$page,$where = []){
        $res = self::where($where)->order($orderby)->paginate(['list_rows'=>$limit,'page'=>$page])->toArray();
        $data = [];

        foreach ($res['data'] as $k => $v){
            $level = MemberLevelModel::getDetails($v['level_id']);

            $v['level_name'] = $level ? $level['name'] : '';
            $v['type_name'] = self::type_name($v['type']);

            $data[$k] = $v;
        }
        $res['data'] = $data;
        return $res;
    }

    /**
     * 添加抽奖次数
     * @param $mid  用户ID
     * @param $source   来源
     * @param $level_id 用户等级
     * @param $remarks  备注
     */
    public static function add($mid,$source,$level_id,$remarks = ''){
        $data = [
            'mid'   =>  $mid,
            'source'    =>  $source,
            'level_id'  =>  $level_id,
            'remarks'   =>  $remarks,
            'add_time'  =>  date('Y-m-d H:i:s',time()),
            'update_time'  =>  date('Y-m-d H:i:s',time()),
        ];
        $id = self::insertGetId($data);
        return $id;
    }

}